package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.SaveAttendJdbc;
import com.wuseguang.springcloud.quartz.JobOperation;
import com.wuseguang.springcloud.service.SaveAttendService;
import com.wuseguang.springcloud.util.CronUtils;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @描述 每天保存考勤数据
 * @创建人 zm
 * @创建时间 2020/7/8
 */
@Service
public class SaveAttendImpl implements SaveAttendService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JobOperation jobOperation;

    @Override
    public Result saveAttend(String schoolId) {

        //设置开始时间加上学校的id 获取到执行时间 每个学校的保存考勤时间错开处理 防止数据库压力过大
        String minuteTime = DateUtil.getMinuteTime(DateUtil.addtimeMinutes("2020-07-09 00:05", Integer.parseInt(schoolId)));

        //要触发的时间
        List<String> cronList = new ArrayList<>();
        cronList.add(CronUtils.formatDateByPattern2(minuteTime));

        try {
            //添加前先把这个以前的删除了
            jobOperation.remove("SaveAttendTimer", schoolId);
            jobOperation.add("SaveAttendTimer", schoolId, cronList);
        } catch (Exception e) {
            logger.error("保存考勤数据失败", e);
        }
        return new Result();
    }
}
